package com.android.lovingpet.home.view;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.android.lovingpet.R;
import com.android.lovingpet.adopt.adoption.adopt.view.PetDetailsActivity;
import com.android.lovingpet.adopt.adoption.view.AdoptionActivity;
import com.android.lovingpet.adopt.host.hostinfo.view.HostInfoActivity;
import com.android.lovingpet.adopt.host.view.HostActivity;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.bean.Body;
import com.android.lovingpet.bean.Host;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.home.IMineContract;
import com.android.lovingpet.home.MinePresenter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ikata on 2018/2/7.
 */
public class AdoptionFragment extends BaseFragment<IMineContract.View, MinePresenter> implements IMineContract.View, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rv_adopt_content)
    RecyclerView rvAdoptContent;
    AdoptionRvAdapter rvAdapter;
    @BindView(R.id.srl_rv)
    SwipeRefreshLayout srlRv;
    @BindView(R.id.iv_host)
    ImageView ivHost;
    @BindView(R.id.iv_adopt)
    ImageView ivAdopt;
    private Unbinder unbinder;

    @Override
    protected int setContentView() {
        return R.layout.fragment_adoption;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        initView();
    }

    @Override
    public MinePresenter createPresenter() {
        return new MinePresenter(this);
    }

    @Override
    public IMineContract.View createView() {
        return this;
    }

    public void initView() {
        rvAdoptContent.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        rvAdoptContent.setItemAnimator(new DefaultItemAnimator());
        rvAdapter = new AdoptionRvAdapter();
        rvAdapter.setOnItemClickListener(position -> {
            Body body = rvAdapter.getData(position);
            if (body instanceof Pet) {
                Intent intent = new Intent(getContext(), PetDetailsActivity.class);
                intent.putExtra("data", body);
                startActivity(intent);
            }
            if (body instanceof Host) {
                EventBus.getDefault().postSticky(body);
                startActivity(new Intent(getContext(), HostInfoActivity.class));
            }

        });
        rvAdoptContent.setAdapter(rvAdapter);
        srlRv.setColorSchemeColors(Color.RED);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        srlRv.setDistanceToTriggerSync(200);
        srlRv.setProgressBackgroundColorSchemeColor(Color.WHITE);
        // 设置圆圈的大小
        srlRv.setSize(SwipeRefreshLayout.DEFAULT);
        srlRv.setOnRefreshListener(this);

        getPresenter().loadingInfo(1, 5);
    }


    @OnClick({R.id.iv_adopt, R.id.iv_host})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_host:
                startActivity(new Intent(getActivity(), HostActivity.class));
                break;
            case R.id.iv_adopt:
                startActivity(new Intent(getActivity(), AdoptionActivity.class));
                break;
            case R.id.publish:

                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {

        rvAdapter.clear();
        getPresenter().loadingInfo(1, 5);
        srlRv.setRefreshing(false);
    }

    @Override
    public void sign(int days) {

    }

    @Override
    public void addInfo(Body body) {
        runOnUiThread(() -> rvAdapter.add(body));
    }
}
